/**
 * Created by rekey on 12/8/13.
 */
(function($) {
    var mediaBtn = $('#blog-media').find('button');
    var mediaInput = $('#blog-media-input');
    var box = mediaBtn.parent().next();
    var form = $('form');
    var page = 1;
    var musicTpl = $('#music-tpl')[0].text;
    var musicTplLayout = $('#music-tpl-layout')[0].text;
    box.delegate('p', 'click', function() {
        this.innerHTML = '<input value="' + this.innerHTML + '" />';
        this.getElementsByTagName('input')[0].focus();
    });
    box.delegate('input', 'click', function() {
        return false;
    });
    box.delegate('input', 'blur', function() {
        this.parentNode.innerHTML = this.value;
    });
    box.delegate('button', 'click', function() {
        var obj = {};
        box.find('div').each(function() {
            var div = $(this);
            obj[div.find('b').html()] = div.find('p').html();
        });
        $.post('/media/create?type=' + mediaBtn.prev().val(), obj, function(resp) {
            form[0]['media']['value'] = resp._id;
            alert('media saved');
        });
        return false;
    });
    box.delegate('tbody tr', 'click', function() {
        box.find('tbody tr.current').removeClass('current');
        this.className = 'current';
        var td = this.getElementsByTagName('td');
        form[0]['title']['value'] = td[1].innerHTML + ' - ' + td[3].innerHTML;
        form[0]['tags']['value'] = td[3].innerHTML;
        form[0]['cover']['value'] = td[6].getElementsByTagName('img')[0].src;
        var obj = {};
        box.find('thead td').each(function(index) {
            if (index == 6) {
                obj[this.innerHTML] = td[index].getElementsByTagName('img')[0].src;
            } else {
                obj[this.innerHTML] = td[index].innerHTML;
            }
        });
        $.post('/media/create?type=music', obj, function(resp) {
            form[0]['media']['value'] = resp._id;
            alert('media saved');
        });
        return false;
    });
    mediaBtn.on('click', function() {
        var type = mediaBtn.prev().val();
        var url = '/media?type=' + type + '&u=' + mediaInput.val() + '&page=' + page;
        $.get(url, function(resp) {
            if (type == 'video') {
                box.html('');
                for (var key in resp) {
                    box.append('<div><b>' + key + '</b><p>' + resp[key] + '</p></div>');
                    if (key == 'title') {
                        form[0]['title']['value'] = resp[key];
                    }
                    if (key == 'tags') {
                        form[0]['tags']['value'] = resp[key];
                    }
                    if (key == 'cover') {
                        form[0]['cover']['value'] = resp[key];
                    }
                    if (key == 'description') {
                        form[0]['text']['value'] = resp[key];
                    }
                }
                box.append('<button>save</button>');
            }
            if (type == 'music') {
                /** @namespace resp.total */
                console.log(resp.total);
                box.html(musicTplLayout);
                var tbody = box.find('tbody');
                $.each(resp.results, function(index) {
                    var str = musicTpl;
                    for (var key in this) {
                        if (key == 'album_logo') {
                            this[key] = this[key].replace('_1', '_2');
                        }
                        str = str.replace('{' + key + '}', this[key]);
                    }
                    tbody.append(str);
                });
            }
        });
        return false;
    });
})(jQuery);